#----------------------------------------------------------------------
# makefile - for Robot WinAvr development
#
#
#----------------------------------------------------------------------
# ENVIRONMENT DEFS
#----------------------------------------------------------------------
include /cygdrive/c/robotconfig.mk

#----------------------------------------------------------------------
# PROJECT DEFS
#----------------------------------------------------------------------
NAME 					= firmware
TERM_BAUDRATE	= 115200

#----------------------------------------------------------------------
# TOOL DEFINITIONS
#----------------------------------------------------------------------
AVR_COMPORT = com2
JAL					= "$(ROBOTROOT_UX)/jal/jalv2/jalv2.exe"
JALLIB			= -long-start -s "$(ROBOTROOT_WIN)\jal\jalv2\lib"
JALSRC		= $(NAME).jal
MACRO       = m4
PROGRAMMER	= $(ROBOTROOT_UX)/jal/xwisp2/xwisp2w port $(JAL_COMPORT) go 
TARGET     	= $(NAME).hex
# downloader
#DOWNLOADER  = avrdude -p m128 -c stk500 -P $(AVR_COMPORT) -U $(NAME).hex -F
# icp
DOWNLOADER  = $(ROBOTROOT_UX)/winavr/bin/avrdude -V -p m324p -c stk500v2 -P $(AVR_COMPORT) -U $(NAME).hex
TERMPRG 		= $(ROBOTROOT_UX)/jal/bootloader/pdload.exe port $(AVR_COMPORT) termspeed $(TERM_BAUDRATE) TERM_RTSON TERMINAL

#----------------------------------------------------------------------
#----------------------------------------------------------------------
# vanaf hier geinspireerd door winavr
#----------------------------------------------------------------------

OBJ            = 	firmware.o        \
                  analyser_io.o     \
                  sampling.o        \
                  messages.o        \
                  uart.o            \
						
MCU_TARGET     = atmega324p
OPTIMIZE       = -Os

DEFS           =
LIBS           =

# You should not have to change anything below here.

CC             = $(ROBOTROOT_UX)/winavr/bin/avr-gcc
CXX             = $(ROBOTROOT_UX)/winavr/bin/avr-g++

CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -IAVRlib -I. -I./libs -DFLOAT_SUPPORT
CXXFLAGS      = $(CFLAGS)

LDFLAGS       = -Wl,-Map,$(NAME).map

OBJCOPY        = $(ROBOTROOT_UX)/winavr/bin/avr-objcopy
OBJDUMP        = $(ROBOTROOT_UX)/winavr/bin/avr-objdump

all: $(NAME).elf lst text 

$(NAME).elf: $(OBJ) 
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

clean:
	rm -rf *.o $(NAME).elf *.eps *.png *.pdf *.bak
	rm -rf $(OBJ)
	rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)

lst:  $(NAME).lst

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

deploy:
	$(DOWNLOADER)

terminal:
	$(TERMPRG)
	
# Rules for building the .text rom images

text: hex bin srec

hex:  $(NAME).hex
bin:  $(NAME).bin
srec: $(NAME).srec

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.elf
	$(OBJCOPY) -j .text -j .data -O srec $< $@

%.bin: %.elf
	$(OBJCOPY) -j .text -j .data -O binary $< $@

# Rules for building the .eeprom rom images

eeprom: ehex ebin esrec

ehex:  $(NAME)_eeprom.hex
ebin:  $(NAME)_eeprom.bin
esrec: $(NAME)_eeprom.srec

%_eeprom.hex: %.elf
	-$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%_eeprom.srec: %.elf
	-$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@

%_eeprom.bin: %.elf
	-$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@

# Every thing below here is used by avr-libc's build system and can be ignored
# by the casual user.

JPEGFILES               = stdiodemo-setup.jpg

JPEG2PNM                = jpegtopnm
PNM2EPS                 = pnmtops
JPEGRESOLUTION          = 180
EXTRA_CLEAN_FILES       = *.hex *.bin *.srec *.eps

dox: ${JPEGFILES:.jpg=.eps}

%.eps: %.jpg
	$(JPEG2PNM) $< |\
	$(PNM2EPS) -noturn -dpi $(JPEGRESOLUTION) -equalpixels \
	> $@
